package servlet;

import entity.Passage;
import service.CommonService;
import service.FieldService;
import service.PassageService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet( name = "IndexServlet")
public class IndexServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //使用到的services
        CommonService commonService = new CommonService();
        PassageService passageService = new PassageService();
        FieldService fieldService = new FieldService();

        //获取课程名称
        String courseId = request.getParameter("courseId");
        String courseName = null;
        //如果是null表示不是第一次进入，Dao中已有courseId，否则需要传给Dao课程的courseId以获取
        if(courseId == null)
            courseName = commonService.getCourseName();
        else
            courseName = commonService.getCourseName(courseId);
        request.setAttribute("courseName", courseName);

        //获取课程介绍
        Passage IntroducePassage = passageService.getPassageByField("1");//课程简介fieldID = 1
        String courseIntroduce = null;
        //如果该课程还没有课程介绍
        if(IntroducePassage == null)
            courseIntroduce = "暂无";
        else if(IntroducePassage.getContent().length() > 245)
            courseIntroduce = IntroducePassage.getContent().substring(0, 245) + "...";
        request.setAttribute("courseIntroduce", courseIntroduce);

        //获取课程负责人
        Passage PrincipalPassage = passageService.getPassageByField("2");//课程负责人fieldID = 2
        String coursePrincipal = null;
        //如果该课程还没有课程负责人
        if(PrincipalPassage == null)
            coursePrincipal = "暂无";
        else if(PrincipalPassage.getContent().length() > 140)
            coursePrincipal = PrincipalPassage.getContent().substring(0, 140) + "...";
        request.setAttribute("coursePrincipal", coursePrincipal);

        //获取课程动态
        List<Passage> dynamic_states = passageService.getFieldPassages("16", 1);//课程动态fieldID = 16
        //index首页的课程动态只能容纳最多9个动态，所以多的予以删除
        while(dynamic_states.size() > 9)
            dynamic_states.remove(dynamic_states.size() - 1);
        request.setAttribute("dynamic_states", dynamic_states);

        //获取课程组成员
        List<Passage> members = passageService.getFieldPassages("3",1);
        request.setAttribute("members", members);

        //获取友情链接
        List<Passage> links = passageService.getFieldPassages("4", 1);
        request.setAttribute("links", links);

        //获取底部信息
        Passage bottomPassage = passageService.getPassageByField("5");
        String bottom_content = (bottomPassage == null) ? "暂无" : bottomPassage.getContent();
        request.setAttribute("bottom_content", bottom_content);

        request.getRequestDispatcher("index.jsp").forward(request, response);
    }
}
